//该文件专门用于创建整个应用的路由器
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../pages/Login-test.vue'
import Home from '../pages/Home-test.vue'
import Welcome from '../pages/Welcome-test.vue'
import Users from '../pages/User-test.vue'
import Rights from '../pages/Rights-test.vue'
import Roles from '../pages/Roles-test.vue'
import Categories from '../pages/Categories-test.vue'
import Params from '../pages/Params-test.vue'
import Goods from '../pages/Goods-test.vue'
import Add from '../pages/Add-test.vue'
import Order from '../pages/Order-test.vue'
import Report from '../pages/Report-test.vue'
import updateOrders from '../pages/updateOrders-test.vue'
Vue.use(VueRouter)
//创建一个路由器
const router = new VueRouter({
    routes: [
        {
            path: '/',//如果用户访问/根路径时，自动刷新重定向到login页面
            redirect: '/login'//自动刷新重定向到login页面
        },
        {
            path: '/login',
            component: Login,
        },
        {
            path: '/home',
            component: Home,
            redirect: '/welcome',//自动刷新重定向到welcome页面
            children:[
                {
                    path:'/welcome',
                    component:Welcome,
                },
                {
                    path:'/users',
                    component:Users,
                },
                {
                    path:'/rights',
                    component:Rights,
                },
                {
                    path:'/roles',
                    component:Roles,
                },
                {
                    path:'/categories',
                    component:Categories,
                },
                {
                    path:'/params',
                    component:Params,
                },
                {
                    path:'/goods',
                    component:Goods,
                },
                {
                    path:'/goods/add',
                    component:Add,
                },
                {
                    path:'/orders',
                    component:Order,
                },
                {
                    name:'updateOrders-test',
                    path:'/orders/updateOrders/:id',
                    component:updateOrders,
                },
                {
                    path:'/reports',
                    component:Report,
                },
            ]
        },
    ]
})
//挂载前置路由导航守卫进行监测
//如果用户没有登录，但是直接通过url访问特定页面，需要重新导航到登录页面
router.beforeEach((to, from, next) => {
    //to将要访问的路径
    //from代表从哪个路径跳转而来
    //next是一个函数表示放行
    if (to.path === '/login') {
        return next()
    } else {
        const tokenStr = window.sessionStorage.getItem('token')
        if (!tokenStr) {//如果没有token则强制跳转到login页面
            return next('/login')
        } else {
            next()
        }
    }
})
//暴露路由
export default router